from google.appengine.ext import db
import logging
import copy

class DocumentMetadata(db.Model):
      # rec-XXXXXXX-XXX
    id = db.StringProperty(verbose_name="Document ID", required=True)
      # gs://glp-rec-library/rec-XXXXXXX-XXX.EXT
    gs_uri = db.StringProperty(verbose_name="Google Storage URI", required=True)
      # http://...
    storage_uri = db.CategoryProperty(verbose_name="Data Location", required=True)
      # http://... or file://...
    source_uri = db.StringProperty(verbose_name="Data Source", required=False)
      # Mime Type
    content_type = db.CategoryProperty(verbose_name="Data Format", required=True)
    title = db.TextProperty(verbose_name="Title", required=True)
    authors = db.StringListProperty(verbose_name="Author(s)", required=True)
    sources = db.StringListProperty(verbose_name="Source(s)", required=True)
    description = db.TextProperty(verbose_name="Description", required=False)
    year = db.IntegerProperty(verbose_name="Publication Year", required=False)
    labels = db.StringListProperty(verbose_name="Labels", required=True, indexed=False)

    def __init__(self, *args, **kwargs):
        if not 'key' in kwargs:
            kwargs['key_name'] = kwargs['id']
        db.Model.__init__(self, *args, **kwargs)

    def storage_type(self):
        storage = filter(lambda label: label.startswith('storage:'), self.labels)
        if len(storage) != 1: raise KeyError("multiple storage types")
        return storage[0][8:]

    def status(self):
        status = filter(lambda label: label.startswith('status:'), self.labels)
        if len(status) != 1: raise KeyError("multiple statuses")
        return status[0][7:]

    def topic_labels(self):
        return [label for label in self.labels if label.startswith('topic:')]

    def all_labels(self):
        return [label for label in self.labels if not label.startwith('storage:') and not label.startwith('status:')]

    def clone(self, new_id):
        return DocumentMetadata(
            id = new_id,
            gs_uri = self.gs_uri,
            storage_uri = self.storage_uri,
            source_uri = self.source_uri,
            content_type = self.content_type,
            title = self.title,
            authors = copy.deepcopy(self.authors),
            sources = copy.deepcopy(self.sources),
            description = self.description,
            year = self.year,
            labels = copy.deepcopy(self.labels))

    def WriteToPrinter(self, printer):
        printer.StatusUpdate('  ID:           ' + self.id)
        printer.StatusUpdate('  GS URI:       ' + self.gs_uri)
        printer.StatusUpdate('  STORAGE URI:  ' + self.storage_uri)
        printer.StatusUpdate('  SOURCE URI:   ' + str(self.source_uri))
        printer.StatusUpdate('  CONTENT TYPE: ' + self.content_type)
        printer.StatusUpdate('  TITLE:        ' + self.title)
        printer.StatusUpdate('  AUTHORS:      ' + ' / '.join(self.authors))
        printer.StatusUpdate('  SOURCES:      ' + ' / '.join(self.sources))
        printer.StatusUpdate('  DESCRIPTION:  ' + self.description)
        printer.StatusUpdate('  YEAR:         ' + str(self.year))
        printer.StatusUpdate('  LABELS:       ' + ' / '.join(self.labels))
